package com.ruoyi.common.utils;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 自动生成一个序列号
 *
 * @author: liuchuanfa
 * @since: 2024/03/22
 */
@Component
public class SerialNumberGenerator {
//    private static final DateTimeFormatter DATE_TIME_FORMAT = DateTimeFormatter.ofPattern("yyMMddHHmmssSSS");
    private static final DateTimeFormatter DATE_TIME_FORMAT = DateTimeFormatter.ofPattern("-MMddHHmmss");
    private static final AtomicInteger SEQUENCE = new AtomicInteger(0);

    /**
     * 参数是业务名称
     *
     * @param businessLetter
     * @return {@link String }
     * @author liuchuanfa
     * @since 2024/03/22
     */
    public String generateSerialNumber(String businessLetter) {
        LocalDateTime now = LocalDateTime.now();
        String dateTime = now.format(DATE_TIME_FORMAT);
        int seq = SEQUENCE.incrementAndGet();
//        return businessLetter + dateTime + String.format("%04d", seq);
        return businessLetter + dateTime + String.format("%01d", seq);
    }

}